package com.example.demo.zb;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

/**
 * @author stone
 * @version $$Id: HttpConnectionPool, v 0.1 17/4/24 下午4:45 stone Exp $$
 */
public class HttpsConnectionPool {

    private static CloseableHttpClient httpsClient;

    static {
        httpsClient = createHttpsClient();
    }

    private HttpsConnectionPool() {
        super();
    }

    public static CloseableHttpClient getHttpsClient(String url) {
        return httpsClient;
    }

    public static CloseableHttpClient getHttpsClient() {
        return httpsClient;
    }


    public static CloseableHttpClient createHttpsClient() {
        SSLContext sslContext = null;
        try {
            sslContext = SSLContexts.custom().loadTrustMaterial(null, (TrustStrategy) (x509Certificates, s) -> true).build();
        } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException ignore) {
        }
        return HttpClients.custom().setSSLContext(sslContext)
                .setSSLHostnameVerifier(new NoopHostnameVerifier())
                .build();
    }

}
